package com.babo72.android.gcm.sample;

import java.io.IOException;

import com.google.android.gcm.server.Constants;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;

public class GCMSender {
	
	public static void main(String[] args) throws IOException {
		
		String apiKey = "AIzaSyBEj_s2N4INd-6PJjiywpejw32CGPwMWHI";
		String regId = "APA91bG0ZfwGj9WdlYWHE497I6_wwn5dI3yO26DByXu_aZw5nWDgi4_rHVzg5PE8OYqfsXMaGF8TEUFaDcgUvhofvJRbEknacZ5Oyuc2o4rOl3MRHuc8wrVglHgdo84mZLPVfNSO57HXYxEwGxCGRXmJyv0aEhu-OQ";
		String msg = "msg";

		Sender sender = new Sender(apiKey);
		Message message = new Message.Builder()
				.addData("msg", msg)
				.build();
		Result result = sender.send(message, regId, 1);

		if (result.getMessageId() != null) {
			String canonicalRegId = result.getCanonicalRegistrationId();
			if (canonicalRegId != null) {
				// same device has more than on registration ID: update database
			} else {
				System.out.println("send OK");
			}
		} else {
			String error = result.getErrorCodeName();
			if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
				// application has been removed from device - unregister database
				System.out.println("device is removed from GCM");
			}
		}
	}
}
